<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/1/10-14:28
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\controller;

    use app\admin\model\Uploads;
    use Jrk\Zipdown;
    use think\Exception;
    use think\facade\Request;

    class Common extends AdminBase
    {
        public function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub
        }


        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/10
         * @name: changeStatus
         * @describe:修改表字段值
         */
        public function changeStatus(){
            if(IS_AJAX) {
                $data = $this->request->post();
                try {
                    if (empty($data['table'])){
                        $this->error("表名不能为空");
                    }
                    $table = $data['table'] ? $data['table'] : "";
                    $res   = db("$table")->where(["id" => (int)$data['id']])->update([$data['field'] => $data['status']]);

                    parent::setGlobals();
                    self::$log->setTitle("修改单个字段值");
                    self::$log->setContent("修改【".$data['table']."】表【".$data['field']."】字段值为【".$data['status']."】");

                    if($res!==false) {

                        $this->success("操作成功");
                    }
                    else {
                        $this->error("操作失败");
                    }
                } catch (Exception $exception) {
                   $this->error($exception->getMessage());
                }
            }
        }



        /**
         * @return array|bool
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: UpImages
         * @describe:上传图片
         */
        public function UpImages()
        {
            return model("uploads")->upFileNoSave("images");
        }


        /**
         * @return false|string
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: Webploader
         * @describe:webuploader上传图片
         */
        public function Webploader(){
            $up = new Uploads();
            $res= $up->upfile("webuploader");
            if($res['code']==0){
                return  $res['msg'];
            }else{
                return json_encode($res['path']);
            }

        }


        /**
         * @return array|bool
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: UpFile
         * @describe:上传文件
         */
        public function UpFile()
        {
            $up = new Uploads();
            return $up->upfile("files");
        }


        /**
         * @param int $with
         * @param int $heig
         * @return array
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: UpThumbImg
         * @describe:生成素略图
         */
        public function UpThumbImg()
        {
            $param = Request::instance()->param();//接收宽高参数
            $with  = 250;
            $heig  = 400;
            if(isset($param['width']) && !empty($param['width'])) {
                $with = $param['width'];
            }
            if(isset($param['height']) && !empty($param['height'])) {
                $heig = $param['height'];
            }
            $up = new Uploads();
            return $up->uploadThumb($with, $heig);
        }




        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/13
         * @name: down_file
         * @describe:打包下载
         */
        public function down_files(){
            $ids=$this->request->param("ids");
            try{
                $data=db("upload")->where(["id"=>['in',$ids]])->select();
                $down=new Zipdown(); //实例化下载类
                //下面是实例操作过程：
                $dfile = tempnam('/tmp', 'tmp');//产生一个临时文件，用于缓存下载文件
                $filename = date ( 'YmdHis' ) . ".zip"; // 最终生成的文件名（含路径）
                //p($picarray);die;
                foreach($data as $val){
                    $down->add_file(file_get_contents(ROOT_PATH.'public'.$val['file_path']), iconv('UTF-8','gbk',$val['filename']));
                }
                $down->output($dfile);

                ob_clean();
                header('Pragma: public');
                header('Last-Modified:'.gmdate('D, d M Y H:i:s') . 'GMT');
                header('Cache-Control:no-store, no-cache, must-revalidate');
                header('Cache-Control:pre-check=0, post-check=0, max-age=0');
                header('Content-Transfer-Encoding:binary');
                header('Content-Encoding:none');
                header('Content-type:multipart/form-data');
                header('Content-Disposition:attachment; filename="'.$filename.'"'); //设置下载的默认文件名
                header('Content-length:'. filesize($dfile));

                $fp = fopen($dfile, 'r');
                while(connection_status() == 0 && $buf = @fread($fp, 8192)){
                    echo $buf;
                }
                fclose($fp);
                @unlink($dfile);
                @flush();
                @ob_flush();
                ob_end_clean();
                exit();
                //打包下载结束
            }catch (Exception $exception){
                $this->error($exception->getMessage());
            }
        }




    }